
#include"base64.h"
int END_OF_INPUT = -1;
char base64Chars[64] = {
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
};

char* base64Str = NULL;
int base64Count;

void setBase64Str(char* str)
{
    base64Str = str;
    base64Count = 0;
}

int readBase64(void)
{
    if (!base64Str) return END_OF_INPUT;
    if (base64Count >= strlen(base64Str)) return END_OF_INPUT;
    char c = base64Str[base64Count] & 0xff;
    base64Count++;
    return c;
 }

QString encodeBase64(char* str)
{
    setBase64Str(str);
    QString result ;
    char inBuffer[3];
    int lineCount = 0;
    bool done = false;
    while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT)
    {
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != END_OF_INPUT)
        {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != END_OF_INPUT)
            {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            }
            else
            {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        }
        else
        {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76)
        {
        result += '\n';
        lineCount = 0;
        }
    }
    return result;
}

//int main(void)
//{
//    char* p ="http:##v.youku.com/v_show/id_XNDAwMTA1NDIw.html";
//    cout<<encodeBase64(p)<<endl;

//}
